#include <iostream>
#include <mutex>

class Singleton {
private:
    static Singleton* instance;
    static std::mutex mtx;
    
    // 禁用外部构造/拷贝/赋值
    Singleton() { std::cout << "Singleton created\n"; }
    Singleton(const Singleton&) = delete;
    Singleton& operator=(const Singleton&) = delete;
public:
    ~Singleton() { std::cout << "Singleton destroyed\n"; }

    // 双检锁线程安全
    static Singleton* getInstance() {
        if (instance == nullptr) {
            std::lock_guard<std::mutex> lock(mtx);
            if (instance == nullptr) {
                instance = new Singleton();
            }
        }
        return instance;
    }

    void log(const std::string& msg) {
        std::cout << "Log: " << msg << std::endl;
    }
};

// 静态成员初始化
Singleton* Singleton::instance = nullptr;
std::mutex Singleton::mtx;

// 使用示例
int main() {
    Singleton::getInstance()->log("System started");
    Singleton::getInstance()->log("User logged in");
    // 手动释放单例（实际中可能不需要）
    // delete Singleton::getInstance(); 
}
